package com.yangwp.doutu.service.impl;

import com.yangwp.doutu.dao.CommentDao;
import com.yangwp.doutu.dto.CommentRespDto;
import com.yangwp.doutu.model.Comment;
import com.yangwp.doutu.service.CommentService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import java.util.*;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDao commentDao;

    @Override
    public Boolean add(String userId, String content, String md5){
        Comment comment = new Comment();
        comment.setCommenterId(userId);
        comment.setContent(content);
        comment.setImg(md5);
        try {
            return commentDao.insert(comment) > 0;
        }catch (Exception e){
            System.out.println(e);
        }
        return Boolean.FALSE;
    }

    @Override
    public List<CommentRespDto> get(String md5){

        try {
            return commentDao.getCommentList(md5);
        }catch (Exception e){
            System.out.println(e);
        }
        return null;
    }

    @Override
    public Integer getCommentNum(String image){
        if(StringUtils.length(image) != 32){
            return 0;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("img", image);
        try {
            return commentDao.countComment(param);
        }catch (Exception e){
            System.out.println(e);
            return 0;
        }
    }

}
